package ar.com.android.alco.forms;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import ar.com.android.alco.entities.DatosUsuario;
import ar.com.android.alco.entities.Version;
import ar.com.android.alco.providers.DatosUsuarioProvider;
import ar.com.android.alco.providers.VersionProvider;
import ar.com.android.alco.utils.AppGlobalVariables;
import ar.com.android.alco.utils.Constants;
import ar.com.android.alco.utils.SyncDatabase;

import com.google.ads.AdView;

public class ALCOActivity extends Activity  implements Runnable {
	private static final String TAG = "ALCOActivity";
	private static final int iDBErrorConect = -1;
	
	ProgressDialog dialog;
	protected AdView adView;
	
    @Override
    public void onStart()
    {
    	super.onStart();
    }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                	
    	dialog = ProgressDialog.show(ALCOActivity.this, "", getString(R.string.actualizandoDatosMsg), true);
		Thread thread = new Thread(this);
		thread.start();
    }
    
    public void onClickCreditos(View v) throws Exception
    {
		Intent intentCreditos = new Intent(this, CreditosActivity.class);
		startActivityForResult(intentCreditos, 1);
    }
    
    public void onClickDatosUsuario(View v) throws Exception
    {
		Intent intentDatosUsuario = new Intent(this, DatosUsuarioActivity.class);
		startActivityForResult(intentDatosUsuario, 1);
    }
    
    public void onClickControlPeso(View v) throws Exception
    {
		Intent intentControlPeso = new Intent(this, ControlPesoActivity.class);
		startActivityForResult(intentControlPeso, 1);
    }
    
    public void onClickRegistro(View v) throws Exception
    {
		Intent intentRegistroCalendario = new Intent(this, RegistroCalendarioActivity.class);
		startActivityForResult(intentRegistroCalendario, 1);
    }

	public void run() {
		Looper.myLooper(); 
	    Looper.prepare();
	    
	    try {
	    	
	    	VersionProvider versionProvider = new VersionProvider(ALCOActivity.this);
	    	Integer versionCode = SyncDatabase.getVersionALCO(this);
	    	if(versionProvider.getTableEmpty()) {
	    		SyncDatabase.syncDatabaseAlimentos(this, versionCode);
	    	}
	    	else {
	    		Version version = versionProvider.getLastVersion();
		    	if(!version.getVersion().equals(versionCode))
		    		SyncDatabase.syncDatabaseAlimentos(this, versionCode);
	    	}
		    	
	    	//Make Ident Datos Usuario Main Form
	    	DatosUsuarioProvider datosUsuarioProvider = new DatosUsuarioProvider(ALCOActivity.this);
	    	
	    	if(datosUsuarioProvider.getTableEmpty()) {
	    		Intent intentDatosUsuario = new Intent(this, DatosUsuarioActivity.class);
	    		startActivityForResult(intentDatosUsuario, 1);
	    	}
	    	else {
	    		DatosUsuario datosUsuario = datosUsuarioProvider.getDatosUsuario(Constants.ID_USUARIO);
	    		if(datosUsuario.getIdUsuarioALCO() == 0)
	    			SyncDatabase.syncDatabaseUsuario(this, datosUsuario);
	    		
	    		AppGlobalVariables.getInstance().setIdUsuario(datosUsuario.getIdDatosUsuario());
				AppGlobalVariables.getInstance().setNombreCompleto(datosUsuario.getNombreApellido());
				AppGlobalVariables.getInstance().setEmail(datosUsuario.getEmail());
				AppGlobalVariables.getInstance().setIdUsuarioALCO(datosUsuario.getIdUsuarioALCO());
				
				SyncDatabase.syncDatabaseControlPeso(this);
	    	}

	    	dialog.dismiss();
	    	
	    } catch (Exception e) {
			Log.e(TAG, e.getMessage());
			Message msg = Message.obtain();
            msg.what = iDBErrorConect; 
            msg.obj = e; 
            handler.sendMessage(msg);
		}
	}
	
	private void updateActivity(Message msg)
	{
		if (msg.what == iDBErrorConect) { 			
    		LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.drawable.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

			TextView text = (TextView) layout.findViewById(R.id.text);
			text.setText(getText(R.string.errorSyncDB));

			Toast toast = new Toast(getApplicationContext());
			toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(layout);
			toast.show();
		}
		dialog.dismiss();
	}
	
	private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	updateActivity(msg);
        }
	};
}